//
//  PMJobOrderListViewController.m
//  ERP
//
//  Created by goldeney on 15/1/12.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "PMJobOrderListViewController.h"
#import "MJRefresh.h"
#import "PMJobOrderListViewCell.h"
#import "UITableView+Custom.h"

#import "WebServiceCommonRequest.h"

#import "NSKeyedArchiverUserLogin.h"
#import "NSDictionary+DataFormat.h"
#import "PMJobOrderDetailViewController.h"

@interface PMJobOrderListViewController ()

@property (nonatomic,strong) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *resultData;
@property (strong, nonatomic) NSString *RWDZTString;

@property (strong, nonatomic) UIView *searchMainView;
@property (strong, nonatomic) UITableView *searchTableView;

@end

@implementation PMJobOrderListViewController
@synthesize tableView = _tableView;
@synthesize searchMainView = _searchMainView;
@synthesize searchTableView = _searchTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"任务单"];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithQueryStyle:(NSInteger)style
{
    self = [super init];
    if(self){
        //任务单状态：0全部，1正供，2待供，3供毕
        if (style == 1) {
            self.RWDZTString = [NSString stringWithFormat:@"正供"];
            [self setTitle:@"正供任务单"];
        } else if (style == 2) {
            self.RWDZTString = [NSString stringWithFormat:@"待供"];
            [self setTitle:@"待供任务单"];
        } else if (style == 3) {
            self.RWDZTString = [NSString stringWithFormat:@"供毕"];
            [self setTitle:@"供毕任务单"];
        } else {
            self.RWDZTString = @"全部";
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.resultData = [NSMutableArray array];
    
    self.tableView.tableFooterView= [[UIView alloc] init];
    [self.tableView registerClass:[PMJobOrderListViewCell class] forCellReuseIdentifier:@"PMJobOrderListViewCell"];
    [self setupRefresh];
}

- (void)dealloc
{
    NSLog(@"MJTableViewController--dealloc---");
}

- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:0];
    NSNumber *endNumber = [NSNumber numberWithInteger:10];
    [self getNetWorkDataIsReresh:YES state:stateNumber end:endNumber];
}

- (void)footerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:self.resultData.count + 1];
    NSNumber *endNumber = [NSNumber numberWithInteger:self.resultData.count + 10];
    [self getNetWorkDataIsReresh:NO state:stateNumber end:endNumber];
}

#pragma mark 网络获取数据

- (void)getNetWorkDataIsReresh:(BOOL)isReresh state:(NSNumber*)state end:(NSNumber*)end
{
    //@状态（全部、供毕、正供、待供）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:@"SCDD_HOME_LIST_ALL"];
    [content setStationID:self.stationID];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"起始条数" value:state];
    [content addParameterKey:@"结束条数" value:end];
    [content addParameterKey:@"状态" value:self.RWDZTString];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(isReresh) [self.tableView headerEndRefreshing];
        else [self.tableView footerEndRefreshing];
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(firstArray && firstArray.count>0){
            if(isReresh) [self.resultData removeAllObjects];
            [self.resultData addObjectsFromArray:firstArray];
            [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            [self.tableView reloadData];
        } else if(isReresh){
            [self.tableView setTableViewNotDataDrawText:@"暂无任务单" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"无法连接网络"];
    }];
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PMJobOrderListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PMJobOrderListViewCell"];
    
    NSDictionary *itemData = [self.resultData objectAtIndex:indexPath.row];
    cell.gcmcLabel.text = [itemData objectFormatStringForKey:@"工程名称" defaults:@"未知"];
    cell.ztLabel.text = [itemData objectFormatStringForKey:@"状态" defaults:@"未知"];
    cell.slLabel.text = [itemData objectFormatStringForKey:@"数量" defaults:@"未知"];
    cell.qddjLabel.text = [itemData objectFormatStringForKey:@"砼强度" defaults:@"未知"];
    cell.jzfsLabel.text = [itemData objectFormatStringForKey:@"浇筑方式" defaults:@"未知"];
    cell.sgdwLabel.text = [itemData objectFormatStringForKey:@"施工单位" defaults:@"未知"];
    cell.sgbwLabel.text = [itemData objectFormatStringForKey:@"施工部位" defaults:@"未知"];
    cell.gddzLabel.text = [itemData objectFormatStringForKey:@"工地地址" defaults:@"无地址"];
    
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *itemData = [self.resultData objectAtIndex:indexPath.row];
    
    PMJobOrderDetailViewController *viewController = [[PMJobOrderDetailViewController alloc] initWithValue:itemData];
    [viewController setStationName:self.stationName StationID:self.stationID];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end
